package com.liduanwei.springboot.controller

import com.baomidou.mybatisplus.mapper.EntityWrapper
import com.baomidou.mybatisplus.plugins.Page
import com.liduanwei.springboot.entity.UsCurrentVersion
import com.liduanwei.springboot.service.IUsCurrentVersionService
import com.jinrustar.op.core.base.BaseController
import com.jinrustar.op.core.support.Assert
import com.jinrustar.op.core.util.InstanceUtil
import com.jinrustar.op.core.util.Request2ModelUtil
import com.jinrustar.op.core.util.WebUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.ui.ModelMap
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.servlet.http.HttpServletRequest

@RestController
@Api(description = "机器端各当前版本", value = "机器端各当前版本")
@RequestMapping(value = "version/current")
class UsVersionCurrentController : BaseController() {
    @Autowired
    private val usCurrentVersionService: IUsCurrentVersionService? = null


    @ApiOperation("查看")
    @RequestMapping(value = "/list", method = arrayOf(RequestMethod.GET, RequestMethod.POST))
    fun list(request: HttpServletRequest, modelMap: ModelMap,
             @ApiParam(value = "页码") @RequestParam(value = "pageNum", required = false, defaultValue = "1") pageNum: Int,
             @ApiParam(value = "size") @RequestParam(value = "pageSize", required = false, defaultValue = "50") pageSize: Int): Any {
        val params = WebUtil.getParameterMap(request)

        return setSuccessModelMap(modelMap, usCurrentVersionService!!.queryAll(params))
    }


    @ApiOperation("更新当前版本")
    @RequestMapping(value = "/{machineId}/update", method = arrayOf(RequestMethod.POST))
    fun update(request: HttpServletRequest, modelMap: ModelMap,
               @PathVariable(value = "machineId") machineId: String,
               @ApiParam(value = "httpServiceVersionName") @RequestParam(value = "httpServiceVersionName", required = false) httpServiceVersionName: String?,
               @ApiParam(value = "httpServiceVersionCode") @RequestParam(value = "httpServiceVersionCode", required = false) httpServiceVersionCode: Integer?,

               @ApiParam(value = "machineVersionName", required = false) @RequestParam(value = "machineVersionName", required = false) machineVersionName: java.lang.String?,
               @ApiParam(value = "machineVersionCode") @RequestParam(value = "machineVersionCode", required = false) machineVersionCode: Integer?,

               @ApiParam(value = "uiVersionName") @RequestParam(value = "uiVersionName", required = false) uiVersionName: String?,
               @ApiParam(value = "uiVersionCode") @RequestParam(value = "uiVersionCode", required = false) uiVersionCode: Integer?,

               @ApiParam(value = "otherFilesVersionName") @RequestParam(value = "otherFilesVersionName", required = false) otherFilesVersionName: String?,
               @ApiParam(value = "otherFilesVersionCode") @RequestParam(value = "otherFilesVersionCode", required = false) otherFilesVersionCode: Integer?,

               @ApiParam(value = "selfVersionName") @RequestParam(value = "selfVersionName", required = false) selfVersionName: String?,
               @ApiParam(value = "selfVersionCode") @RequestParam(value = "selfVersionCode", required = false) selfVersionCode: Integer?
    ): Any {

        val currentVersion: UsCurrentVersion = Request2ModelUtil.covert(UsCurrentVersion::class.java, request)

        val currentVersionPage: Page<UsCurrentVersion> = usCurrentVersionService!!.queryAll(InstanceUtil.newHashMap("machineId", machineId))

        if (currentVersionPage.total != 0) { /*已存在*/
            val cv = currentVersionPage.records[0]
            currentVersion.id = cv.id
            currentVersion.createTime = cv.createTime
        } else {
            currentVersion.machineId = machineId
        }

        val result = usCurrentVersionService.insertOrUpdateWithDefaultValue(currentVersion)
        return setSuccessModelMap(modelMap, result)
    }
}